package com.ys.service.storage.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.storage.TccLocation;
import com.ys.entity.storage.TccWarehouse;
import com.ys.mapper.storage.TccLocationMapper;
import com.ys.service.storage.TccLocationService;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Map;

/**
 * 货位管理实现层
 */
@Service("tccLocationService")
public class TccLocationaServiceImpl implements TccLocationService {
    private static final Logger logger = LoggerFactory.getLogger(TccLocationaServiceImpl.class);


    @Autowired
    private TccLocationMapper tccLocationMapper;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {
        if (!params.get("feeStandard").toString().matches("\\d+(\\.\\d{0,2})?")){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return R.error("占库费需要是数值，两位小数点！");
        }
        String ckq = params.get("ckq").toString();
        String vhw = params.get("vhw").toString();
        if (ckq == null || vhw == null) {
            return R.error("请补充必填项");
        } else if (new TccLocation().setVhw(vhw).setCkq(ckq).count() > 0) {
            return R.error("不符合货位名称-仓库名称-库区名称唯一");
        }
        TccLocation entity = JSON.parseObject(JSON.toJSONString(params), TccLocation.class);
        tccLocationMapper.insert(entity);
        return R.ok(AddDto.closeAndReload());
    }
    @Override
    @Transactional
    public R edit(Map<String, Object> params) throws Exception {
        TccLocation entity = JSON.parseObject(JSON.toJSONString(params), TccLocation.class);
        tccLocationMapper.update(entity);
        return R.ok("操作成功");
    }
}
